package servlet;

import util.JDBCUtil;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //功能1：用户交互
        String username=req.getParameter("username");
        String pwd=req.getParameter("pwd");
        System.out.println(req.getRemoteUser());
        //如何访问数据库
        String sql="select * from employee where username=? and PASSWORD=?";
        List params=new ArrayList<>();
        params.add(username);
        params.add(pwd);
        ResultSet rs=JDBCUtil.execQuery(sql,params);
        //功能：控制流转,如果rs不为空，就说明在数据库里成功找到了一个人，用户名和密码都匹配
        try {
            if(rs.next())
                //登录成功
            {
                //获取session对象
                HttpSession session=req.getSession();
                //设置session有效的变量
                session.setAttribute("username",username);


               resp.sendRedirect("IndexServlet");
            }
                else
                    //登录失败
                req.getRequestDispatcher("fail.html").forward(req,resp);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setCharacterEncoding("gbk");
        System.out.println(req.getRequestURI());
        resp.getWriter().println("get方法");

    }
}
